package com.rongshu.view.userInfo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.rongshu.R;
import com.rongshu.utils.DateUtils;
import com.rongshu.view.home.listener.OnItemClickListener;
import com.rongshu.view.userInfo.entity.Recommend;
import com.widget.CircleImageView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RecommendAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext = null;

    private List<Recommend.ResponseBean> recommendList;

    //interface
    private OnItemClickListener listener = null;

    public RecommendAdapter(Context context) {
        this.mContext = context;
    }

    public void setRecommendList(List<Recommend.ResponseBean> list) {
        this.recommendList = list;
    }

    public void addRecommendList(List<Recommend.ResponseBean> list) {
        this.recommendList.addAll(list);
    }

    public List<Recommend.ResponseBean> getRecommendList() {
        return this.recommendList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_recommend_item_view, parent, false);
        return new RecommenViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        Recommend.ResponseBean responseBean = recommendList.get(position);
        RecommenViewHolder holder = (RecommenViewHolder) viewHolder;
        //头像
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .placeholder(R.drawable.user_head_icon);
        Glide.with(mContext)
                .load(responseBean.getAvatar())
                .apply(options)
                .into(holder.iv_head_icon);
        //vip
        holder.iv_vip.setVisibility(responseBean.isVip() ? View.VISIBLE : View.GONE);
        //bi_name
        holder.txt_article_author.setText(responseBean.getBi_name());
        //time
        holder.txt_article_time.setText(DateUtils.ConvertMonth(responseBean.getCreate_datetime()));
        //title
        holder.txt_article_title.setText(responseBean.getTitle());
        //content
        holder.txt_article_content.setText(responseBean.getContents_header());
        //hot
        holder.txt_article_hot.setText(responseBean.getOne());
        //hint
        holder.txt_article_hint.setText(responseBean.getZhu());
        //listener
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onItemClick(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return recommendList == null ? 0 : recommendList.size();
    }

    public class RecommenViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_head_icon)
        CircleImageView iv_head_icon;
        @BindView(R.id.iv_vip)
        ImageView iv_vip;
        @BindView(R.id.txt_article_author)
        TextView txt_article_author;
        @BindView(R.id.txt_article_time)
        TextView txt_article_time;
        @BindView(R.id.txt_article_title)
        TextView txt_article_title;
        @BindView(R.id.txt_article_content)
        TextView txt_article_content;
        @BindView(R.id.txt_article_hot)
        TextView txt_article_hot;
        @BindView(R.id.txt_article_hint)
        TextView txt_article_hint;

        public RecommenViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    /**
     * set listener
     */
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
}
